package com.retailshop.shopuser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.retailshop.shopuser.common.HttpPost;
import com.retailshop.shopuser.entity.HttpRequestConfig;
import com.retailshop.shopuser.entity.Operatorcost;
import com.retailshop.shopuser.mapper.OperatorcostMapper;
import com.retailshop.shopuser.service.OperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("OperatorService")
public class OperatorServerImpl  implements OperatorService {

    @Autowired
    OperatorcostMapper operatorcostMapper;

    @Override
    public Boolean Operator() {

        //读取运营成本
        num = 0;
        //开始计算
        System.out.println("开始计算");
        operatorcostMapper.updateJs();
        System.out.println("完成");
        Operator1();
        return true;
    }

    private int num;

    private Boolean Operator1() {

        num++;

        System.out.println("第" + num + "轮计算.");

        int count = operatorcostMapper.updateJs1(num);

        System.out.println("完成第" + num + "轮计算:" + count);

        if (count > 0) {
            Operator1();
        }

        return true;
    }

    @Override
    public Boolean Operatorsettlement() {
        System.out.println("开始结算.");
        List<Operatorcost> operatorcostList = operatorcostMapper.Operatorsettlement();
        if (operatorcostList != null) {
            for (int i = 0; i < operatorcostList.size(); i++) {
                Operatorsettlement1(operatorcostList.get(i));
            }
        }
        System.out.println("完成结算.");
        return true;
    }

    private boolean Operatorsettlement1(Operatorcost operatorcost) {
        System.out.println("开始结算:" + operatorcost.getUsernoId());

        List<Operatorcost> operatorcostList = operatorcostMapper.selectUserNoList1(operatorcost.getUsernoId());
        double rebates=0;
        if (operatorcostList != null) {
            for (int i = 0; i < operatorcostList.size(); i++) {
                rebates+=Operatorsettlementresult(operatorcostList.get(i).getOperatorcost());
            }
        }

        //结果
        double userRebates = Operatorsettlementresult(operatorcost.getOperatorcost())-rebates;

        System.out.println("用户" + operatorcost.getUsernoId()+":"+userRebates);

        //添加到数据表
        if (userRebates > 0) {
            String xonsumptionRes = HttpPost.GetMod(HttpRequestConfig.finaceServerUrl, "/Finace/Add", "classify=1&type=151&state=0&userId=" + operatorcost.getUsernoId() + "&UID=" + operatorcost.getUsernoId() + "&amount=" + userRebates + "&remark=5月销售返点");
            JSONObject xonsumptionResJSON = JSONObject.parseObject(xonsumptionRes);
            String msgCode = xonsumptionResJSON.get("code").toString();
            if (!msgCode.equals("000000")) {//如果失败
                return false;
            }
        }

        return true;
    }

    private double Operatorsettlementresult(double operator) {
        System.out.println("运营成本:" + operator);
        double rebates = 0;
        {
            if (operator > 10000000) {
                rebates = operator * 70 * 0.01;
            }
            else if (operator > 5000000) {
                rebates = operator * 69.5 * 0.01;
            }
            else if (operator > 3000000) {
                rebates = operator * 69 * 0.01;
            }
            else if (operator > 2000000) {
                rebates = operator * 68.5 * 0.01;
            }
            else if (operator > 1000000) {
                rebates = operator * 68 * 0.01;
            }
            else if (operator > 500000) {
                rebates = operator * 67.5 * 0.01;
            }else if (operator > 300000) {
                rebates = operator * 67 * 0.01;
            }else if (operator > 250000) {
                rebates = operator * 667 * 0.01;
            }else if (operator > 200000) {
                rebates = operator * 65 * 0.01;
            }else if (operator > 100000) {
                rebates = operator * 64 * 0.01;
            }else if (operator > 75000) {
                rebates = operator * 63 * 0.01;
            }else if (operator > 50000) {
                rebates = operator * 62 * 0.01;
            }else if (operator > 35000) {
                rebates = operator * 61 * 0.01;
            }else if (operator > 20000) {
                rebates = operator * 60 * 0.01;
            }else if (operator > 10000) {
                rebates = operator * 58.5 * 0.01;
            }else if (operator > 7500) {
                rebates = operator * 56.5 * 0.01;
            }else if (operator > 7000) {
                rebates = operator * 54.5 * 0.01;
            }else if (operator > 3000) {
                rebates = operator * 52.5 * 0.01;
            }else if (operator > 2000) {
                rebates = operator * 50 * 0.01;
            }else if (operator > 1000) {
                rebates = operator * 47.5 * 0.01;
            }else if (operator > 500) {
                rebates = operator * 45 * 0.01;
            }else if (operator > 200) {
                rebates = operator * 45 * 0.01;
            }else if (operator > 100) {
                rebates = operator * 35 * 0.01;
            }else if (operator > 50) {
                rebates = operator * 30 * 0.01;
            }
        }
        return rebates;
    }

}